<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/08/07 05:20:29 $)
// $RCSfile: functions_cli.php,v $ : $Revision: 1.2 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

// Certain code parts taken from Horde Framework. For more information please visit http://www.horde.org

/**
 * Horde_CLI:: API for basic command-line functionality/checks.
 *
 * $Horde: framework/CLI/CLI.php,v 1.34 2004/03/01 22:04:51 chuck Exp $
 *
 * Copyright 2003-2004 Chuck Hagenbuch <chuck@horde.org>
 * Copyright 2003-2004 Jan Schneider <jan@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @author  Jan Schneider <jan@horde.org>
 * @version Revision: 1.34
 * @since   Horde 3.0
 * @package Horde_CLI
*/

define ("CLI_INFO", 1);
define ("CLI_ERROR", 2);
define ("CLI_OK", 3);
define ("CLI_WARNING", 4);

/**
* Command Line Functions
*/
class CLI
{
	var $_newline = "";
	var $_indent = "";

    var $_bold_start = "";
    var $_bold_end = "";

    var $_red_start = "";
    var $_green_start = "";
    var $_yellow_start = "";
    var $_blue_start = "";

    var $_red_end = "";
    var $_green_end = "";
    var $_yellow_end = "";
    var $_blue_end = "";

	/**
	* Constructor
	*/
	function CLI()
	{
		$this->_newline = "\n";
		$this->_indent  = '    ';

		$term = getenv('TERM');
		if ($term) {
			if (preg_match('/^(xterm|vt220|linux)/', $term)) {
				$this->_bold_start   = "\x1b[1m";
				$this->_red_start    = "\x1b[01;31m";
				$this->_green_start  = "\x1b[01;32m";
				$this->_yellow_start = "\x1b[01;33m";
				$this->_blue_start   = "\x1b[01;34m";
				$this->_bold_end = $this->_red_end = $this->_green_end = $this->_yellow_end = $this->_blue_end = "\x1b[0m";
			} elseif (preg_match('/^vt100/', $term)) {
				$this->_bold_start = "\x1b[1m";
				$this->_bold_end   = "\x1b[0m";
			}
		}
	}

	/**
	* Prints $text on a single line.
	*
	* @param string $text  The text to print.
	* @param bool $pre     If true the linebreak is printed before
	*                      the text instead of after it.
	*/
	function writeln($text = '', $pre = false)
	{
		if ($pre)
		{
			echo $this->_newline . $text;
		} else {
			echo $text . $this->_newline;
		}
	}

	/**
	* Returns the indented string.
	*
	* @param string $text  The text to indent.
	*/
	function indent($text)
	{
		return $this->_indent . $text;
	}

	/**
	* Returns a bold version of $text.
	*
	* @param string $text  The text to bold.
	*/
	function bold($text)
	{
		return $this->_bold_start . $text . $this->_bold_end;
	}

	/**
	* Returns a red version of $text.
	*
	* @param string $text  The text to print in red.
	*/
	function red($text)
	{
		return $this->_red_start . $text . $this->_red_end;
	}

	/**
	* Returns a green version of $text.
	*
	* @param string $text  The text to print in green.
	*/
	function green($text)
	{
		return $this->_green_start . $text . $this->_green_end;
	}

	/**
	* Returns a blue version of $text.
	*
	* @param string $text  The text to print in blue.
	*/
	function blue($text)
	{
		return $this->_blue_start . $text . $this->_blue_end;
	}

	/**
	* Returns a yellow version of $text.
	*
	* @param string $text  The text to print in yellow.
	*/
	function yellow($text)
	{
		return $this->_yellow_start . $text . $this->_yellow_end;
	}

	/**
	* Displays a message.
	*
	* @param string $event          The message string.
	* @param optional string $type  The type of message: 'cli.error',
	*                               'cli.warning', 'cli.success', or
	*                               'cli.message'.
	*/
	function message($message, $type = CLI_MESSAGE)
	{
		global $_SWIFT;

		if ($_SWIFT["language"]["clierror"] != "")
		{
			$clierror = $_SWIFT["language"]["clierror"];
		} else {
			$clierror = "[ERROR]: ";
		}

		if ($_SWIFT["language"]["cliwarning"] != "")
		{
			$cliwarning = $_SWIFT["language"]["cliwarning"];
		} else {
			$cliwarning = "[WARNING]: ";
		}

		if ($_SWIFT["language"]["cliok"] != "")
		{
			$cliok = $_SWIFT["language"]["cliok"];
		} else {
			$cliok = "[OK]: ";
		}

		if ($_SWIFT["language"]["cliinfo"] != "")
		{
			$cliinfo = $_SWIFT["language"]["cliinfo"];
		} else {
			$cliinfo = "[INFO]: ";
		}

		switch ($type) {
			case CLI_ERROR:
				$type_message = $this->red($clierror);
				break;
			case CLI_WARNING:
				$type_message = $this->yellow($cliwarning);
				break;
			case CLI_OK:
				$type_message = $this->green($cliok);
				break;
			case CLI_INFO:
				$type_message = $this->blue($cliinfo);
				break;
		}

		$this->writeln($type_message . $message);
	}

	/**
	* Displays a fatal error message.
	*
	* @param string $error  The error text to display.
	*/
	function fatal($error)
	{
		global $_SWIFT;

		$this->writeln($this->red('===================='));
		$this->writeln();
		$this->writeln($this->red($_SWIFT["language"]["clifatalerror"]));
		$this->writeln($this->red($error));
		$this->writeln();
		$this->writeln($this->red('===================='));

		exit;
	}

	/**
	* Prompts for a user response.
	*
	* @param string $prompt            The message to display when
	*                                  prompting the user.
	* @param optional array $choices   The choices available to the
	*                                  user or null for a text input.
	*/
	function prompt($prompt, $choices = null)
	{
		global $_SWIFT;

		$stdin = fopen('php://stdin', 'r');

		// Main event loop to capture top level command.
		while (true) {
			// Print out the prompt message.
			$this->writeln($prompt . ' ', !is_array($choices));
			if (is_array($choices) && !empty($choices)) {
				foreach ($choices as $key => $choice) {
					$key = $this->bold($key);
					$this->writeln('(' . $key . ') ' . $choice);
				}
		
				if ($_SWIFT["language"]["clienterchoice"] != "")
				{
					$enterchoice = $_SWIFT["language"]["clienterchoice"];
				} else {
					$enterchoice = "Please Type your Choice: ";
				}
				$this->writeln($enterchoice, true);

				// Get the user choice.
				$response = trim(fgets($stdin, 256));

				if (isset($choices[$response])) {
					// Close standard in.
					fclose($stdin);
					return $response;
				} else {
					if ($_SWIFT["language"]["clinotvalidchoice"] != "")
					{
						$invalidchoicechoice = $_SWIFT["language"]["clinotvalidchoice"];
					} else {
						$invalidchoicechoice = '"%s" is Not a Valid Choice: ';
					}
					$this->writeln(sprintf($invalidchoice, $response));
				}

			} else {
				$response = trim(fgets($stdin, 256));
				fclose($stdin);
				return $response;
			}
		}

		return false;
	}
}
?>